package com.ycsj.goldmedalnewconcept.activity;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.base.BaseNewActivity;
import com.ycsj.goldmedalnewconcept.utils.UiHelper;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 连接Wifi界面
 * Created by Administrator on 2018/3/1 0001.
 */

public class ConnectWifiActivity extends BaseNewActivity {
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.input_wifiname)
    EditText inputWifiname;
    @Bind(R.id.input_password)
    EditText inputPassword;
    @Bind(R.id.synchronize_wifi_btn)
    Button synchronizeWifiBtn;
    @Bind(R.id.progress_in)
    ProgressBar progressIn;


    private WifiManager wifiMgr;
    private WifiInfo wifiInf;
    private int mLocalIp;
    private String mSSID;

    //用于区分是否是老师还是家长,0代表老师,1代表家长
    private int type = -1;

    @Override
    protected int getLayoutId() {
        return R.layout.connect_wifi_layout;
    }

    @Override
    protected void initView() {
        //获取传递过来的用于区分类型的标致
        type = getIntent().getIntExtra("type", -1);
        //初始配置WIFI名称的方法
        initWiFiState();
    }


    /**
     * 初始配置Wifi的方法
     */
    private void initWiFiState() {
        wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiInf = wifiMgr.getConnectionInfo();

        mLocalIp = wifiInf.getIpAddress();
        mSSID = wifiInf.getSSID();
        if (!TextUtils.isEmpty(mSSID) && mSSID.startsWith("\"")) {
            mSSID = mSSID.substring(1, mSSID.length() - 1);
        }

        inputWifiname.setText(mSSID);
        if (!TextUtils.isEmpty(inputWifiname.getText().toString())) {
            inputPassword.requestFocus();
        }
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @OnClick({R.id.rl_back, R.id.synchronize_wifi_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.synchronize_wifi_btn:
                judgeEmpty();
                break;
        }
    }

    /**
     * 判断输入的wifi名称和密码是否正确
     */
    private void judgeEmpty() {
        //获取输入的Wifi名称
        String wifiName = inputWifiname.getText().toString().trim();
        //获取输入的Wifi密码
        String wifiPwd = inputPassword.getText().toString().trim();
        if (TextUtils.isEmpty(wifiName)) {
            UiHelper.toast(this, "请输入Wifi名称");
            return;
        }
        if (TextUtils.isEmpty(wifiPwd)) {
            UiHelper.toast(this, "请输入Wifi密码");
            return;
        }

        //进行网络请求进行设备的连接
        httpConnection();
    }

    /**
     * 进行连接设备的网络请求
     */
    private void httpConnection() {

        connectByAudio();

    }


    private void connectByAudio() {
//        Task.call(new Callable<JSONObject>() {
//            @Override
//            public JSONObject call() throws Exception {//这里可以返回空值，把返回类型设为Void即可,返回其他类型的用法将在下面提到
//                HashMap<String, Object> param_ = new HashMap<String, Object>();
//                long sec = (System.currentTimeMillis()/1000);
//                param_.put("appid", "xiaoxiaoban");
//                param_.put("timestamp", sec);
//                param_.put("signature", Tools.Encrypt("xiaoxiaoban" + sec +  "xxb_4469fe575896cb15", "")); // (appid+timestamp+app_secret)
//                param_.put("ssid", mSSID);
//
//                if (Tools.isNotEmpty(input_password.getText().toString()))
//                    param_.put("password", input_password.getText().toString().trim());
//
//                LeUser lu = LeXiaoXiaoBanApp.getInstance().getUser();
//                if (lu == null) {
//                    lu = Tools.restoreLeUser();
//                }
//
//                param_.put("extra", lu.id);
//
//                return HttpUtils.startRequestJsonObj("http://conn.dandanman.com/api/app_request_wav_ex", param_,
//                        HttpUtils.TAG_POST_REQUEST);
//            }
//        }, Task.BACKGROUND_EXECUTOR).onSuccess(new Continuation<JSONObject, Object>() {
//
//            @Override
//            public Void then(Task<JSONObject> task) throws Exception {
//                if (task.getResult() != null) {
//                    int ret = task.getResult().getInt("err");
//                    /**
//                     *   1: 参数错误
//                     2: 生成声波失败
//                     3: 签名验证失败
//                     4: 时间戳错误
//                     */
//                    switch (ret) {  //
//                        case 0:
//
//                            JSONArray array =  task.getResult().getJSONArray("wavs");
//                            // strWavs = array.toString().split(",");
//
//                            List<String> list = new ArrayList<String>();
//                            for (int i=0; i<array.length(); i++) {
//                                list.add( array.getString(i) );
//                            }
//                            strWavs = list.toArray(new String[list.size()]);
//
//                            playSound();
//                            break;
//                        case 1:
//                            toastShow("参数错误");
//                            break;
//                        case 2:
//                            toastShow("生成声波失败");
//                            break;
//                        case 3:
//                            toastShow("签名验证失败");
//                            break;
//                        case 4:
//                            toastShow("时间戳错误");
//                            break;
//                    }
//                }
//
////        toWifiHotSpotListActivity();
//
//                return null;
//            }
//        }, Task.UI_THREAD_EXECUTOR);//主线程运行
    }

}
